#include "Deco_Audio.h"
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;
#define INBUF_SIZE 4096
#define AUDIO_INBUF_SIZE 20480
#define AUDIO_REFILL_THRESH 4096

Deco_Audio::Deco_Audio() {


}

void Deco_Audio::register_codecs() {
	av_register_all(); //register the codecs on the system.
}

int Deco_Audio::busca_codec() {
	codec = avcodec_find_decoder(CODEC_ID_MP2);
	if (!codec) {
		cerr << "Encoder no encontrado" << endl;
		return -1;
	}
	return 0;
}

void Deco_Audio::ini_contexto() {
	decoder_Ctx = avcodec_alloc_context();
}

int Deco_Audio::abre_codec() {
	if (avcodec_open(decoder_Ctx, codec) < 0) {
		cerr << "No se pudo abrir el encoder" << endl;
		return -1;
	}
	return 0;
}

int Deco_Audio::decodificar(short **samples, int *outbuf_size, uint8_t **inbuf, int inbuf_size) {
	AVPacket *packet;
	av_init_packet(packet);
	packet->data = *inbuf;
	packet->size = inbuf_size;
	int len = avcodec_decode_audio3(decoder_Ctx, *samples, outbuf_size, packet);
	if(len > 0) {
		av_free(packet);
		return len;
	}
	av_free(packet);
	return -1;
}

int Deco_Audio::getFrameSize() {
	return (decoder_Ctx->frame_size);
}

AVCodecContext* Deco_Audio::getDecoder_Ctx() {
	return decoder_Ctx;
}

Deco_Audio::~Deco_Audio() {
	// TODO Auto-generated destructor stub
}
